package guhong.play.commandsystem.gui.ui;

import guhong.play.commandsystem.gui.MainWindow;
import lombok.Data;

import javax.swing.*;
import java.awt.*;
import java.util.HashMap;
import java.util.Map;

/**
 * UI 管理器
 * 管理所有的UI组件
 *
 * @author : 李双凯
 * @date : 2024/2/29 16:28
 **/
@Data
public class UIManger {

    public static final String UI_NAME_MAIN = "main";
    public static final String UI_NAME_TERMINAL_SCROLL = "terminalScroll";
    public static final String UI_NAME_TERMINAL = "terminal";
    public static final String UI_NAME_COMMAND_INPUT_BOX = "commandInputBox";
    public static final int MAX_TERMINAL_VERTICAL_SCROLL_BAR = 5000;
    private Map<String, Component> uiMap = new HashMap<>();

    public void put(String key, Component ui) {
        uiMap.put(key, ui);
    }

    public Component get(String key) {
        return uiMap.get(key);
    }


    public MainWindow getMain() {
        return (MainWindow) get(UI_NAME_MAIN);
    }


    public JTextArea getTerminal() {
        return (JTextArea) get(UI_NAME_TERMINAL);
    }


    public JScrollPane getTerminalScroll() {
        return (JScrollPane) get(UI_NAME_TERMINAL_SCROLL);
    }

    public JTextField getCommandInputBox() {
        return (JTextField) get(UI_NAME_COMMAND_INPUT_BOX);
    }

    // 终端相关操作

    /**
     * 终端上进行打印
     * 如果短时间内在终端上打印的东西很多，那么内存会飙升
     *
     * @param info 打印的信息
     */
    public void print(String info) {
        JTextArea terminal = getTerminal();
        SwingUtilities.invokeLater(() -> {
            terminal.append(info);
        });

    }

    /**
     * 终端上进行换行打印
     *
     * @param info 打印的信息
     */
    public void println(String info) {
        print(info + "\n");
    }

    /**
     * 清空控制台信息
     */
    public void clearTerminal() {
        SwingUtilities.invokeLater(() -> {
            JTextArea terminal = getTerminal();
            terminal.setText("");

            // 如果滚动条很长，会导致存在大量的Rectangle2D和Point2D对象，内存会飚很高
            JScrollPane terminalScroll = getTerminalScroll();
            JScrollBar verticalScrollBar = terminalScroll.getVerticalScrollBar();
            int verticalScrollBarMaximum = verticalScrollBar.getMaximum();
            if (verticalScrollBarMaximum >= MAX_TERMINAL_VERTICAL_SCROLL_BAR) {
                System.gc();
            }
        });
    }

    /**
     * 滚动到最底部
     */
    public void scrollToBottom() {
        SwingUtilities.invokeLater(() -> {
            JScrollPane terminalScroll = getTerminalScroll();
            JScrollBar verticalScrollBar = terminalScroll.getVerticalScrollBar();
            verticalScrollBar.setValue(verticalScrollBar.getMaximum());
        });
    }

    // 命令输入框相关操作

    /**
     * 输入命令
     * @param str 命令字符
     */
    public void input(String str) {
        SwingUtilities.invokeLater(() -> {
            JTextField commandInputBox = getCommandInputBox();
            commandInputBox.setText(commandInputBox.getText() + str);
        });
    }

    public String getCommand() {
        JTextField commandInputBox = getCommandInputBox();
        return commandInputBox.getText().trim();
    }

    /**
     * 清空命令
     */
    public void clearCommand() {
        SwingUtilities.invokeLater(() -> {
            JTextField commandInputBox = getCommandInputBox();
            commandInputBox.setText("");
        });
    }

    /**
     * 让输入框获得光标焦点
     */
    public void getFocusInWindow() {
        SwingUtilities.invokeLater(() -> {
            getCommandInputBox().requestFocusInWindow();
        });
    }
}
